﻿package com.webbei.servlet;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.apache.log4j.Logger;
import org.apache.velocity.app.Velocity;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.wabbei.fenxiao.action.MyTaobaoUtil;
import com.webbei.Backstage.service.BackstateSer;
import com.webbei.basic.init.InitProduct;
import com.webbei.pojo.Orders;

public class InitServlet extends HttpServlet {
	private static WebApplicationContext springContext;
	private ServletContext context;
//	private ActionContext ac;

	@Override
	public void init() throws ServletException {
		super.init();
		springContext = WebApplicationContextUtils
				.getWebApplicationContext(this.getServletContext());
		InitProduct._servletContext = this.getServletContext();
		context = this.getServletContext();
		InitProduct.getInstance().init();
		List list = InitProduct.list;
		context.setAttribute("product", list);
		System.out.println("init***********************************");
		//new Thread(new OrderSync()).start();
		try {
		SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat formatDate2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		String s = formatDate.format(new Date())+" 23:59";
		System.out.println(s);
		Date firstTime = formatDate2.parse(s);
		System.out.println(firstTime);
		new Timer().schedule(new OrderSyncTask(), firstTime, 24*60*60*1000);
		Properties p = new Properties();
		String real = this.getServletContext().getRealPath("/");
		p.put("runtime.log", real+"velocity.log");
		p.put("file.resource.loader.path", real+"WEB-INF/vm");
		p.put("input.encoding", "UTF-8");
		p.put("output.encoding", "UTF-8");
		p.put("directive.foreach.counter.name","index");
		
			Velocity.init(p);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
/*	class OrderSync implements Runnable{
		public void run() {
			BackstateSer bao = (BackstateSer) springContext.getBean("BackService");
			while(true){
				try {
					Date today = new Date();

				SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
				Date s;
				s = formatDate.parse(formatDate.format(today));
				Date e = new Date(s.getTime()-24*60*60*1000*4);
				SimpleDateFormat formatDate2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String start = formatDate2.format(e);
				String end = formatDate2.format(s);
				System.out.println(start+","+end);
				List<Orders> oList = new MyTaobaoUtil().getFullOrderList(start, end);
				int num = bao.syncOrders(oList);
				Logger log = Logger.getLogger(this.getClass());
				log.info(today+"：同步"+num+"条订单");
				System.out.println(today+"：同步"+num+"条订单");
					Thread.sleep(12*60*60*1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				} catch (ParseException e) {
					e.printStackTrace();
				}
			}
		}

	}*/
	class OrderSyncTask extends TimerTask{
		public void run() {
			System.out.println("开始任务......");
			BackstateSer bao = (BackstateSer) springContext.getBean("BackService");
			
				try{
					Date today = new Date();

				SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
				Date s;
				s = formatDate.parse(formatDate.format(today));
				Date e = new Date(s.getTime()-24*60*60*1000*5);
				SimpleDateFormat formatDate2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String start = formatDate2.format(e);
				String end = formatDate2.format(s);
				System.out.println(start+","+end);
				List<Orders> oList = new MyTaobaoUtil().getFullOrderList(start, end);
				int num = bao.syncOrders(oList);
				Logger log = Logger.getLogger(this.getClass());
				log.info(today+"：同步"+num+"条订单");
				System.out.println(today+"：同步"+num+"条订单");
				
				}catch(Exception e){
					e.printStackTrace();
				}
			
		}
	}

}
